package com.snippeteer.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.snippeteer.domain.User;
import com.snippeteer.infrastructure.presistence.UserRepository;
import com.snippeteer.model.SnippetUser;

@Controller
@RequestMapping("/user")
public class SnippetUserController {
	
	@Autowired
	UserRepository userRepository;
	
	
	@RequestMapping(value = "user/{idStr}", method = RequestMethod.GET)
	public @ResponseBody SnippetUser getUserInJSON(@PathVariable("idStr") final String idStr) {
		Integer id = Integer.valueOf(idStr);
		
		System.out.println("id " + id);
		User user = userRepository.getUser(id);
		
		SnippetUser sUser = new SnippetUser();
		sUser.setId(user.getId());
		sUser.setUsername(user.getUsername());
		sUser.setPassword(user.getPassword());
		
		return sUser;

	}
	
	@RequestMapping(value="user/{username}/{password}", method = RequestMethod.POST)
	public @ResponseBody SnippetUser createUser(@PathVariable("username") final String username,
										 @PathVariable("password") final String password) {

		User user = userRepository.saveUser(username, password);
		
		SnippetUser sUser = new SnippetUser();
		sUser.setId(user.getId());
		sUser.setUsername(user.getUsername());
		sUser.setPassword(user.getPassword());
		
		return sUser;

	}
}
